package com.zhiyume.controller;

import com.zhiyume.api.util.ApiClassifyEnum;
import com.zhiyume.dao.entity.TInformation;
import com.zhiyume.dao.entity.TInformationWithBLOBs;
import com.zhiyume.oss.OssUpload;
import com.zhiyume.service.InformationService;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.util.TypeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/information")
public class InformationController {
    @Autowired
    private InformationService informationService;


    /**
     * 获取所有分类的资讯列表
     * @param page
     * @param pageSize
     * @param title
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value="/getInformationList")
    @ResponseBody
    public Object getInformationList(@RequestParam(defaultValue = "0") int page,
                               @RequestParam(defaultValue = "10") int pageSize,
                               @RequestParam(defaultValue = "") String title,
                               @RequestParam(defaultValue = "") String navType,
                               @RequestParam(defaultValue = "") String startTime,
                               @RequestParam(defaultValue = "") String endTime){
        Map dataMap = informationService.findAllInformationList(page,pageSize,title,navType,startTime,endTime);
        return dataMap;
    }

    @RequestMapping("/informationListPage")
    public ModelAndView informationListPage(){
        ModelAndView modelAndView = new ModelAndView();
        List navList = ApiClassifyEnum.getNavList();
        modelAndView.addObject("navList", navList);
        modelAndView.setViewName("page/information/informationList");
        return modelAndView;
    }


    @RequestMapping("/informationListPhonePage")
    public ModelAndView informationListPhonePage(){
        ModelAndView modelAndView = new ModelAndView();
        List navList = ApiClassifyEnum.getNavList();
        modelAndView.addObject("navList", navList);
        modelAndView.setViewName("page/information/informationPhoneList");
        return modelAndView;
    }


    /**
     * 资讯新增下拉框初始化
     * @return
     */
    @RequestMapping("/addInformationPage")
    public ModelAndView addInformationPage(){

        TInformation tInformation = new TInformation();
        // 栏目下拉列表
        List navList = ApiClassifyEnum.getNavList();
        // 楼层下拉列表
        //List floorList = Arrays.asList(TypeConstants.floorArr);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("navList", navList);
        //modelAndView.addObject("floorList", floorList);
        modelAndView.addObject("tInformation",tInformation);
        modelAndView.setViewName("page/information/informationAdd");
        return modelAndView;
    }


    /**
     * 资讯新增下拉框初始化
     * @return
     */
    @RequestMapping("/informationOpenListPage")
    public ModelAndView informationOpenListPage(@RequestParam(value="flag",defaultValue="0")String flag){

        TInformation tInformation = new TInformation();
        // 栏目下拉列表
        List navList = ApiClassifyEnum.getNavList();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("navList", navList);
        modelAndView.addObject("tInformation",tInformation);
        modelAndView.addObject("flag",flag);
        modelAndView.setViewName("page/information/informationOpenList");
        return modelAndView;
    }

    /**
     * 新增资讯保存
     * @param tInformation
     * @return
     */
    @RequestMapping("/addInformationN")
    @ResponseBody
    public String addInformationN(@ModelAttribute TInformationWithBLOBs tInformation){
        try {
            informationService.addInformation(tInformation);
        }catch (Exception e){
            if(e.getMessage().contains("too long")){
                JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SYS_ERROR.getErrType(), "图片名称太长");
            }
            return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SYS_ERROR.getErrType(), ConstantEnum.CONSTANT_SYS_ERROR.getErrMsg());
        }
        return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg());
    }


    /**
     * 新增资讯保存
     * @param tInformation
     * @return
     */
    @RequestMapping("/addInformation")
    @ResponseBody
    public Map addInformation(@ModelAttribute("form") TInformationWithBLOBs tInformation){
        String result = "success";
        Map map = new HashMap();
        try {
            informationService.addInformation(tInformation);
        }catch (Exception e){
            result = "fail";

            if(e.getMessage().contains("too long")){
                map.put("msg","图片名称太长");
            }
        }
        map.put("result",result);
        return map;
    }

    /**
     * 资讯编辑页面
     * @return
     */
    @RequestMapping("editInformation")
    public ModelAndView informationEdit(@RequestParam(defaultValue = "0") long id){
        TInformation tInformation = informationService.findInfoWithBlobsById(id);

        // 栏目下拉列表
        List navList = ApiClassifyEnum.getNavList();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("tInformation",tInformation);
        modelAndView.addObject("navList", navList);
        modelAndView.setViewName("page/information/informationEdit");
        return modelAndView;
    }

    /**
     * 资讯详情页面
     * @return
     */
    @RequestMapping("informationDetail")
    public ModelAndView informationDetail(@RequestParam(defaultValue = "0") long id){
        TInformation tInformation = informationService.findInfoWithBlobsById(id);

        // 栏目下拉列表
        List navList = ApiClassifyEnum.getNavList();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("tInformation",tInformation);
        modelAndView.addObject("navList", navList);
        modelAndView.setViewName("page/information/informationDetail");
        return modelAndView;
    }

    /**
     * 资讯详情页面
     * @return
     */
    @RequestMapping("findInformation")
    @ResponseBody
    public TInformation findInformation(@RequestParam(defaultValue = "0") long id){
        TInformation tInformation = informationService.findInfoWithBlobsById(id);

        return tInformation;
    }

    /**
     * 资讯编辑保存
     */
    @RequestMapping("updateInformation")
    @ResponseBody
    public Map informationUpdate(@ModelAttribute("form") TInformationWithBLOBs information){
        String result = "success";
        try {
            informationService.updateInformation(information);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

    /**
     * 删除封面图(重新上传封面图时)
     */
    @RequestMapping("delImgs")
    @ResponseBody
    public Map delImgs(HttpServletRequest request){
        //String[] urls = coverUrl.split(TypeConstants.URL_SPLIT);
        String result = "success";
        String filedir = request.getParameter("filedir");
        String[] imgUrlArr = request.getParameterValues("imgUrlArr[]");
        try {
            for (String url : imgUrlArr){
                //url = url.substring(url.indexOf(filedir),url.indexOf("?"));
                OssUpload.delPic(url);
            }
        } catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }


    /**
     * 返回待审核页面
     * @return
     */
    @RequestMapping("/checkPendingListPage")
    public ModelAndView checkPendingListPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/information/checkPendingList");
        return modelAndView;
    }

    /**
     * 返回已发布页面
     * @return
     */
    @RequestMapping("/publishedListPage")
    public ModelAndView publishedListPage(){
        ModelAndView modelAndView = new ModelAndView();
        List navList = ApiClassifyEnum.getNavList();
        Set keys = TypeConstants.floorValueMap.keySet();
        modelAndView.addObject("navList", navList);
        modelAndView.addObject("statusKeys", keys);
        modelAndView.addObject("statusValue", TypeConstants.floorValueMap);
        modelAndView.setViewName("page/information/publishedList");
        return modelAndView;
    }

    /**
     * 返回审核失败页面
     * @return
     */
    @RequestMapping("/failedListPage")
    public ModelAndView failedListPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/information/failedList");
        return modelAndView;
    }



    /**
     * 获取 待审核资讯列表
     */
    @RequestMapping(value="/getCheckPendingList")
    @ResponseBody
    public Object getCheckPendingList(@RequestParam(defaultValue = "0") int page,
                                     @RequestParam(defaultValue = "10") int pageSize,
                                     @RequestParam(defaultValue = "") String title,
                                     @RequestParam(defaultValue = "") String startTime,
                                     @RequestParam(defaultValue = "") String endTime){
        Map dataMap = informationService.findCheckPendingList(page,pageSize,title,startTime,endTime);
        return dataMap;
    }

    /**
     * 已发布咨询，flag标识是否存视频稿件
     */
    @RequestMapping(value="/getPublishedList")
    @ResponseBody
    public Object getPublishedList(@RequestParam(defaultValue = "0") int page,
                                      @RequestParam(defaultValue = "10") int pageSize,
                                      @RequestParam(defaultValue = "") String title,
                                      @RequestParam(defaultValue = "999") int navType,
                                      @RequestParam(defaultValue = "999") int floorType,
                                      @RequestParam(defaultValue = "") String startTime,
                                      @RequestParam(defaultValue = "") String endTime,
                                   @RequestParam(defaultValue = "") String flag){
        Map dataMap = informationService.findPublishedList(page,pageSize,title,navType,floorType,startTime,endTime,flag);

        return dataMap;
    }


    /**
     * 获取 审核失败资讯列表
     */
    @RequestMapping(value="/getFailedList")
    @ResponseBody
    public Object getFailedList(@RequestParam(defaultValue = "0") int page,
                                      @RequestParam(defaultValue = "10") int pageSize,
                                      @RequestParam(defaultValue = "") String title,
                                      @RequestParam(defaultValue = "") String startTime,
                                      @RequestParam(defaultValue = "") String endTime){
        Map dataMap = informationService.findFailedList(page,pageSize,title,startTime,endTime);
        return dataMap;
    }

    /**
     * 资讯状态更新 {"草稿","待审核","审核通过","已发布","审核未通过"}
     */
    @RequestMapping(value="/updateInformationStatus")
    @ResponseBody
    public Map updateInformationStatus(@RequestParam long id,
                                       @RequestParam String status){
        String result = "success";
        HashMap map = new HashMap();
        try {
            informationService.updateInformationStatus(id,status);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        map.put("result",result);
        return map;

    }

    @RequestMapping(value="/deleteInformationById")
    @ResponseBody
    public Map deleteInformationById(@RequestParam long id){
        String result = "success";
        try {
            informationService.deleteInformationById(id);
        }catch (Exception e){
            result = e.getMessage();
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }


    /**
     * 更新操作相关状态(置顶,推荐,下架,撤回重审)
     * @param id
     * @param statusType
     * @param statusValue
     * @return
     */
    @RequestMapping(value="/updateInformationOptStatus")
    @ResponseBody
    public Map updateInformationOptStatus(@RequestParam long id,
                                       @RequestParam String statusType,
                                       @RequestParam String statusValue){
        String result = "success";
        HashMap map = new HashMap();

        try {
            informationService.updateInformationOptStatus(id,statusType,statusValue);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        map.put("result",result);
        return map;
    }
}
